package com.clw.weblogin.controller;

import com.clw.weblogin.common.entity.ResultEntity;
import com.clw.weblogin.entity.Department;
import com.clw.weblogin.entity.HuanKuan;
import com.clw.weblogin.entity.HuanKuanLiuShui;
import com.clw.weblogin.entity.JieKuanLiuShui;
import com.clw.weblogin.service.DepartmentService;
import com.clw.weblogin.service.HuanKuanLiuShuiService;
import com.clw.weblogin.service.HuanKuanService;
import com.clw.weblogin.service.JieKuanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/huankuanliushui")
public class HuanKuanLiuShuiController {

    public static final String LIUSHUI_SESSION_NAME = "huankuanliushui";
    @Autowired
    HuanKuanLiuShuiService service;
    @Autowired
    DepartmentService departmentService;
    @Autowired
    HuanKuanService huanKuanService;
    @Autowired
    JieKuanService jieKuanService;

    @ResponseBody
    @RequestMapping("/selectByid")
    public ResultEntity selctById(int huankuanid, int page, @RequestParam(required = false, defaultValue = "5") int limit) {
        ResultEntity entity = new ResultEntity();
        HuanKuanLiuShui huanKuanLiuShui = new HuanKuanLiuShui();
        huanKuanLiuShui.setHuankuanid(huankuanid);
        entity = service.selectById(huanKuanLiuShui, page, limit);
        return entity;
    }

    @ResponseBody
    @RequestMapping("/selectCount")
    public ResultEntity selectCount(HttpSession session) {
        ResultEntity entity = new ResultEntity();
        HuanKuanLiuShui huanKuanLiuShui = new HuanKuanLiuShui();
//        if(!departmentid.equals("")&&departmentid!=null){
//            huanKuanLiuShui.setDepartmentid(departmentid);
//        }else {
//        }
        entity = service.selectCount(huanKuanLiuShui);
//        if(200 == entity.getState()){
//            //登录成功，保存用户信息到Session
//            session.setAttribute(LIUSHUI_SESSION_NAME,entity.getData());
//        }
        return entity;
    }

    @ResponseBody
    @RequestMapping("/buhuan")
    public ResultEntity buhuan(String id, String name, String usernumber, String book, String carnumber, String phone, String daikuanmoney, String daikuantype, String daikuanbegindate
            , String lilv, String yuegong, String shoufu, String lastjiexidate, String lastjiebendate, String yue,
                               String danbao, String beizhu, String caozuoren, String departmentid, String huankuantype
            , String yuhuanlixi, String yuhuanbenjin, String shihuanjine, String shihuandate, String lixi, String daikuanenddate, String caozuoleixing) throws Exception {

        ResultEntity entity = new ResultEntity();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        HuanKuanLiuShui bean = new HuanKuanLiuShui();
        bean.setName(name);
        bean.setNamenumber(usernumber);
        bean.setBook(book);
        bean.setPhone(phone);
        bean.setCarnumber(carnumber);
        bean.setDaikuantype(daikuantype);
        bean.setDaikuanmoney(Double.valueOf(daikuanmoney));
        bean.setDaikuanbegindate(sdf.parse(daikuanbegindate));
        bean.setLilv(lilv);
        if (yuegong.equals("")) {
            bean.setYuegong(0.0);
        } else {
            bean.setYuegong(Double.valueOf(yuegong));
        }

        if (lastjiexidate.equals("")) {
            bean.setJiexidate(sdf.parse(daikuanbegindate));
        } else {
            bean.setJiexidate(sdf.parse(lastjiexidate));
        }
        if (lastjiebendate.equals("")) {
            bean.setJiebendate(sdf.parse(daikuanenddate));
        } else {
            bean.setJiebendate(sdf.parse(lastjiebendate));
        }
        if (shoufu.equals("")) {
            bean.setShoufu(0.0);
        } else {
            bean.setShoufu(Double.valueOf(shoufu));
        }
        bean.setBeizhu(beizhu);
        bean.setDaobao(danbao);
        bean.setHuankuantype(huankuantype);
        bean.setCaozuoren(caozuoren);
        bean.setDepartmentid(departmentid);
        Department department = new Department();
        department.setDepartmentid(Integer.valueOf(departmentid));
        bean.setDepartmentname(departmentService.selectnameByid(department));
        bean.setHuankuanid(Integer.valueOf(id));
        bean.setCaozuoleixing(huankuantype);
        int shihuanbenjin = 0;
        int shihuanlixi = 0;

        //算利息
        int days=jieKuanService.days(bean.getJiexidate(), sdf.parse(shihuandate));
        int yinghuanlixi=  huanKuanService.havelixi(days, Double.valueOf(bean.getLilv()), (Double.valueOf(yue)));
        if (yinghuanlixi > 0) {//有利息
            if (Integer.valueOf(shihuanjine) >=yinghuanlixi) { //还款金额 》利息
                //先还利息
                shihuanlixi = yinghuanlixi;
                shihuanbenjin = 0;
                bean.setDaikuanenddate(sdf.parse(daikuanenddate));
                bean.setLixi(shihuanlixi);
                bean.setJiexidate(sdf.parse(shihuandate));
                bean.setYue(Double.valueOf(yue));
                bean.setType(2);
                bean.setCaozuodate(new Date());
                bean.setHuanlixi(Integer.valueOf(shihuanlixi));
                bean.setHuanbenji(0.0);
                bean.setQishu("" + sdf.format(bean.getJiexidate()) + "-" + shihuandate);
                int record = service.insert(bean);
                // 修改还款表
                HuanKuan huanKuan = new HuanKuan();
                huanKuan.setId(Integer.valueOf(id));
                huanKuan.setJiexidate(bean.getJiexidate());
                huanKuan.setLixi(Integer.valueOf(yuhuanlixi) - Integer.valueOf(shihuanlixi));
                entity = huanKuanService.huanlixi(huanKuan);
                if(entity.getState()==200){
                    //还本金
                    shihuanbenjin = Integer.valueOf(shihuanjine) - yinghuanlixi;
                    //查到还本金记录，更新daikuanenddate
                    HuanKuanLiuShui huan1 = new HuanKuanLiuShui();
                    huan1.setType(1);
                    huan1.setHuankuanid(Integer.valueOf(id));
                    if (lastjiebendate == "") {
                        huan1.setJiebendate(sdf.parse(daikuanenddate));
                    } else {
                        huan1.setJiebendate(sdf.parse(lastjiebendate));
                    }
                    List<HuanKuanLiuShui> list = service.selectByExample(huan1, 0, 0);
                    if (list.size() > 0) {
                        bean.setDaikuanenddate(huanKuanService.yujidate(sdf.parse(daikuanenddate), 30));
                        bean.setJiebendate(sdf.parse(shihuandate));
                    }
                    if(shihuanbenjin>Integer.valueOf(yuegong)){// 还好几个月
                        int beishu=Integer.valueOf(shihuanbenjin)/Integer.valueOf(yuegong);
                        for(int i=0;i<beishu;i++){
                            bean.setType(1);
                            bean.setHuankuantype("补还本金");
                            bean.setCaozuoleixing("补还本金");
                            if(shihuanbenjin>Integer.valueOf(yuegong)){
                                bean.setHuanbenji(Double.valueOf(yuegong));
                            }else{
                                bean.setHuanbenji(Double.valueOf(shihuanbenjin));
                            }
                            bean.setHuanlixi(0);
                            if (Double.valueOf(yue) > 0) {
                                bean.setYue(Double.valueOf(yue) - bean.getHuanbenji());
                            } else {
                                bean.setYue(0.0);
                            }
                            bean.setQishu("" + sdf.format(daikuanenddate) + "-" + sdf.format(bean.getDaikuanenddate()));
                            int record1 = service.insert(bean);
                            // 修改还款表
                            HuanKuan huanKuan1 = new HuanKuan();
                            huanKuan1.setId(Integer.valueOf(id));
                            huanKuan1.setDaikuanenddate(bean.getDaikuanenddate());
                            huanKuan1.setJiebendate(bean.getJiebendate());
                            huanKuan1.setYue(bean.getYue());
                            if (huanKuan1.getYue() == 0.0) {
                                huanKuan1.setType(1);
                            }
                            entity = huanKuanService.updatesome(huanKuan1);
                            if(entity.getState()!=200){
                                System.out.println("实还金额大于利息，并大于月供 逻辑报错");
                                break;
                            }else{
                                shihuanbenjin=shihuanbenjin-Integer.valueOf(yuegong);
                            }
                        }
                    }else{//还一个月
                        bean.setType(1);
                        bean.setHuankuantype("补还本金");
                        bean.setCaozuoleixing("补还本金");
                            bean.setHuanbenji(Double.valueOf(shihuanbenjin));
                        bean.setHuanlixi(0);
                        if (Double.valueOf(yue) > 0) {
                            bean.setYue(Double.valueOf(yue) - bean.getHuanbenji());
                        } else {
                            bean.setYue(0.0);
                        }
                        bean.setQishu("" + sdf.format(daikuanenddate) + "-" + sdf.format(bean.getDaikuanenddate()));
                        int record1 = service.insert(bean);
                        // 修改还款表
                        HuanKuan huanKuan1 = new HuanKuan();
                        huanKuan1.setId(Integer.valueOf(id));
                        huanKuan1.setDaikuanenddate(bean.getDaikuanenddate());
                        huanKuan1.setJiebendate(bean.getJiebendate());
                        huanKuan1.setYue(bean.getYue());
                        if (huanKuan1.getYue() == 0.0) {
                            huanKuan1.setType(1);
                        }
                        entity = huanKuanService.updatesome(huanKuan1);
                    }
                }
            } else {                                           //还款金额《利息，
                shihuanlixi = yinghuanlixi;
                shihuanbenjin = 0;
                bean.setDaikuanenddate(sdf.parse(daikuanenddate));
                bean.setLixi(Integer.valueOf(yuhuanlixi) - Integer.valueOf(shihuanlixi));
                bean.setJiexidate(sdf.parse(shihuandate));
                bean.setYue(Double.valueOf(yue));
                bean.setType(2);
                bean.setCaozuodate(new Date());
                bean.setHuanlixi(Integer.valueOf(shihuanlixi));
                bean.setHuanbenji(0.0);
                bean.setQishu("" + sdf.format(bean.getJiexidate()) + "-" + shihuandate);
                int record = service.insert(bean);
                // 修改还款表
                HuanKuan huanKuan = new HuanKuan();
                huanKuan.setId(Integer.valueOf(id));
                huanKuan.setJiexidate(bean.getJiexidate());
                huanKuan.setLixi(Integer.valueOf(yuhuanlixi) - Integer.valueOf(shihuanlixi));
                entity = huanKuanService.huanlixi(huanKuan);
            }
        } else {
            //还本金
            shihuanbenjin = Integer.valueOf(shihuanjine) - yinghuanlixi;
            //查到还本金记录，更新daikuanenddate
            HuanKuanLiuShui huan1 = new HuanKuanLiuShui();
            huan1.setType(1);
            huan1.setHuankuanid(Integer.valueOf(id));
            if (lastjiebendate == "") {
                huan1.setJiebendate(sdf.parse(daikuanenddate));
            } else {
                huan1.setJiebendate(sdf.parse(lastjiebendate));
            }
            List<HuanKuanLiuShui> list = service.selectByExample(huan1, 0, 0);
            if (list.size() > 0) {
                bean.setDaikuanenddate(huanKuanService.yujidate(sdf.parse(daikuanenddate), 30));
                bean.setJiebendate(sdf.parse(shihuandate));
            }
            if(shihuanbenjin>Integer.valueOf(yuegong)){// 还好几个月
                int beishu=Integer.valueOf(shihuanjine)/Integer.valueOf(yuegong);
                for(int i=0;i<beishu;i++){
                    bean.setType(1);
                    bean.setHuankuantype("补还本金");
                    bean.setCaozuoleixing("补还本金");
                    if(shihuanbenjin>Integer.valueOf(yuegong)){
                        bean.setHuanbenji(Double.valueOf(yuegong));
                    }else{
                        bean.setHuanbenji(Double.valueOf(shihuanbenjin));
                    }
                    bean.setHuanlixi(0);
                    if (Double.valueOf(yue) > 0) {
                        bean.setYue(Double.valueOf(yue) - bean.getHuanbenji());
                    } else {
                        bean.setYue(0.0);
                    }
                    bean.setQishu("" + sdf.format(daikuanenddate) + "-" + sdf.format(bean.getDaikuanenddate()));
                    int record1 = service.insert(bean);
                    // 修改还款表
                    HuanKuan huanKuan1 = new HuanKuan();
                    huanKuan1.setId(Integer.valueOf(id));
                    huanKuan1.setDaikuanenddate(bean.getDaikuanenddate());
                    huanKuan1.setJiebendate(bean.getJiebendate());
                    huanKuan1.setYue(bean.getYue());
                    if (huanKuan1.getYue() == 0.0) {
                        huanKuan1.setType(1);
                    }
                    entity = huanKuanService.updatesome(huanKuan1);
                    if(entity.getState()!=200){
                        System.out.println("实还金额大于利息，并大于月供 逻辑报错");
                        break;
                    }else{
                        shihuanbenjin=shihuanbenjin-Integer.valueOf(yuegong);
                    }
                }
            }else{//还一个月
                bean.setType(1);
                bean.setHuankuantype("补还本金");
                bean.setCaozuoleixing("补还本金");
                bean.setHuanbenji(Double.valueOf(shihuanbenjin));
                bean.setHuanlixi(0);
                if (Double.valueOf(yue) > 0) {
                    bean.setYue(Double.valueOf(yue) - bean.getHuanbenji());
                } else {
                    bean.setYue(0.0);
                }
                bean.setQishu("" + sdf.format(daikuanenddate) + "-" + sdf.format(bean.getDaikuanenddate()));
                int record1 = service.insert(bean);
                // 修改还款表
                HuanKuan huanKuan1 = new HuanKuan();
                huanKuan1.setId(Integer.valueOf(id));
                huanKuan1.setDaikuanenddate(bean.getDaikuanenddate());
                huanKuan1.setJiebendate(bean.getJiebendate());
                huanKuan1.setYue(bean.getYue());
                if (huanKuan1.getYue() == 0.0) {
                    huanKuan1.setType(1);
                }
                entity = huanKuanService.updatesome(huanKuan1);
            }
//            bean.setType(1);
//            if (Integer.valueOf(shihuanjine) > Integer.valueOf(yuegong)) { //实还本金 》月供
//                int beishu=Integer.valueOf(shihuanjine)/Integer.valueOf(yuegong);
//                for(int i=0;i<beishu;i++){
//
//                }
//            } else {                                                         //实还本金《月供
//
//            }
        }
        entity.setMessage("补还成功");
        return entity;
    }

    @ResponseBody
    @RequestMapping("/insertbenjin")
    public ResultEntity insertbenjin(String id, String name, String usernumber, String book, String carnumber, String phone, String daikuanmoney, String daikuantype, String daikuanbegindate
            , String lilv, String yuegong, String shoufu, String lastjiexidate, String lastjiebendate, String yue,
                                     String danbao, String beizhu, String caozuoren, String departmentid, String huankuantype
            , String yuhuanlixi, String yuhuanbenjin, String shihuanbenjin, String lixi, String daikuanenddate, String caozuoleixing) throws Exception {

        //init
        ResultEntity entity = new ResultEntity();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        HuanKuanLiuShui bean = new HuanKuanLiuShui();

        //init end
        bean.setName(name);
        bean.setNamenumber(usernumber);
        bean.setBook(book);
        bean.setPhone(phone);
        bean.setCarnumber(carnumber);
        bean.setDaikuantype(daikuantype);
        bean.setDaikuanmoney(Double.valueOf(daikuanmoney));
        bean.setDaikuanbegindate(sdf.parse(daikuanbegindate));
        //daikuanenddate              begin
        //需求  //还本金时   先查看 当月已还本金   （本金+这次还本） 大于月供    enddate=当前月+1    小于月供  enddate 不变
       /* huan1.setHuankuanid(Integer.valueOf(id));
        if(lastjiebendate==""){
            huan1.setJiebendate(sdf.parse(daikuanenddate));
            bean.setJiebendate(sdf.parse(daikuanenddate));
        }else{
            huan1.setJiebendate(sdf.parse(lastjiebendate));
            bean.setJiebendate(sdf.parse(lastjiebendate));
        }
        List<HuanKuanLiuShui> list=service.selectByExample(huan1,0,0);
        double yiHuanMoney=0.0;
        if(list.size()>0){
            for (int i=0;i<list.size();i++){
                yiHuanMoney=yiHuanMoney+list.get(i).getHuanbenji();
            }
        }
        if((yiHuanMoney+Double.valueOf(shihuanbenjin))>=Double.valueOf(yuegong)){
            bean.setDaikuanenddate(huanKuanService.yujidate(sdf.parse(daikuanenddate),30));
            bean.setJiebendate(new Date());
        }else{
            bean.setDaikuanenddate(sdf.parse(daikuanenddate));
            bean.setJiebendate(new Date());
        }*/
        //daikuanenddate  end

        //还款 当前月有还本金记录，结束日期跳下月---begin
        HuanKuanLiuShui huan1 = new HuanKuanLiuShui();
        huan1.setType(1);
        huan1.setHuankuanid(Integer.valueOf(id));
        if (lastjiebendate == "") {
            huan1.setJiebendate(sdf.parse(daikuanenddate));
            bean.setJiebendate(sdf.parse(daikuanenddate));
        } else {
            huan1.setJiebendate(sdf.parse(lastjiebendate));
            bean.setJiebendate(sdf.parse(lastjiebendate));
        }
        List<HuanKuanLiuShui> list = service.selectByExample(huan1, 0, 0);
        if (list.size() > 0) {
            bean.setDaikuanenddate(huanKuanService.yujidate(sdf.parse(daikuanenddate), 30));
            bean.setJiebendate(new Date());
        }
        //还款 当前月有还本金记录，结束日期跳下月---end
        bean.setLilv(lilv);
        bean.setYuegong(Double.valueOf(yuegong));
        bean.setJiexidate(sdf.parse(lastjiexidate));
        if (shoufu.equals("")) {

        } else {
            bean.setShoufu(Double.valueOf(shoufu));
        }
        if (Double.valueOf(yue) > 0) {
            bean.setYue(Double.valueOf(yue) - Double.valueOf(shihuanbenjin));
        } else {
            bean.setYue(0.0);
        }
        bean.setCaozuoleixing(caozuoleixing);
        bean.setBeizhu(beizhu);
        bean.setDaobao(danbao);
        bean.setHuankuantype(huankuantype);
        bean.setType(1);
        bean.setCaozuoren(caozuoren);
        bean.setDepartmentid(departmentid);
        Department department = new Department();
        department.setDepartmentid(Integer.valueOf(departmentid));
        bean.setDepartmentname(departmentService.selectnameByid(department));
        bean.setLixi(Integer.valueOf(yuhuanlixi));
        bean.setCaozuodate(new Date());
        bean.setHuankuanid(Integer.valueOf(id));
        bean.setCaozuoleixing(huankuantype);
        bean.setHuanlixi(0);
        bean.setHuanbenji(Double.valueOf(shihuanbenjin));
        bean.setQishu("" + sdf.format(daikuanenddate) + "-" + sdf.format(bean.getDaikuanenddate()));
        int record = service.insert(bean);
        // 修改还款表
        HuanKuan huanKuan = new HuanKuan();
        huanKuan.setId(Integer.valueOf(id));
        huanKuan.setDaikuanenddate(bean.getDaikuanenddate());
        huanKuan.setJiebendate(bean.getJiebendate());
        huanKuan.setYue(bean.getYue());
        if (huanKuan.getYue() == 0.0) {
            huanKuan.setType(1);
        }
        entity = huanKuanService.updatesome(huanKuan);
        return entity;
    }

    @ResponseBody
    @RequestMapping("/insertlixi")
    public ResultEntity insertlixi(String id, String name, String usernumber, String book, String carnumber, String phone, String daikuanmoney, String daikuantype, String daikuanbegindate
            , String lilv, String yuegong, String shoufu, String lastjiexidate, String lastjiebendate, String yue,
                                   String danbao, String beizhu, String caozuoren, String departmentid, String huankuantype
            , String yuhuanlixi, String yuhuanbenjin, String shihuanlixi, String lixi, String daikuanenddate, String caozuoleixing) throws Exception {

        //init
        ResultEntity entity = new ResultEntity();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        HuanKuanLiuShui bean = new HuanKuanLiuShui();
        //init end
        bean.setName(name);
        bean.setNamenumber(usernumber);
        bean.setBook(book);
        bean.setPhone(phone);
        bean.setCarnumber(carnumber);
        bean.setDaikuantype(daikuantype);
        bean.setDaikuanmoney(Double.valueOf(daikuanmoney));
        bean.setDaikuanbegindate(sdf.parse(daikuanbegindate));
        bean.setDaikuanenddate(sdf.parse(daikuanenddate));
        bean.setLilv(lilv);
        if (yuegong.equals("")) {
            bean.setYuegong(0.0);
        } else {
            bean.setYuegong(Double.valueOf(yuegong));
        }
        bean.setJiexidate(new Date());
        if (lastjiebendate.equals("")) {
            bean.setJiebendate(sdf.parse(daikuanenddate));
        } else {
            bean.setJiebendate(sdf.parse(lastjiebendate));
        }
        if (shoufu.equals("")) {
            bean.setShoufu(0.0);
        } else {
            bean.setShoufu(Double.valueOf(shoufu));
        }
        bean.setBeizhu(beizhu);
        bean.setYue(Double.valueOf(yue));
        bean.setDaobao(danbao);
        bean.setHuankuantype(huankuantype);
        bean.setType(2);
        bean.setCaozuoren(caozuoren);
        bean.setDepartmentid(departmentid);
        Department department = new Department();
        department.setDepartmentid(Integer.valueOf(departmentid));
        bean.setDepartmentname(departmentService.selectnameByid(department));
        Integer yu = Integer.valueOf(yuhuanlixi);
        Integer shi = Integer.valueOf(shihuanlixi);
        Integer as = yu - shi;
        bean.setLixi(Integer.valueOf(yuhuanlixi) - Integer.valueOf(shihuanlixi));
        bean.setCaozuodate(new Date());
        bean.setHuankuanid(Integer.valueOf(id));
        bean.setCaozuoleixing(huankuantype);
        bean.setHuanlixi(Integer.valueOf(shihuanlixi));
        bean.setHuanbenji(0.0);
        bean.setQishu("" + sdf.format(bean.getJiexidate()) + "-" + sdf.format(new Date()));
        int record = service.insert(bean);
        // 修改还款表
        HuanKuan huanKuan = new HuanKuan();
        huanKuan.setId(Integer.valueOf(id));
        huanKuan.setJiexidate(bean.getJiexidate());
        huanKuan.setLixi(Integer.valueOf(yuhuanlixi) - Integer.valueOf(shihuanlixi));
        entity = huanKuanService.huanlixi(huanKuan);
        return entity;
    }
}
